/*
 * @Description  :
 * @Autor        : guoxi
 * @Date         : 2022-10-27 17:11:26
 * @LastEditors  : Please set LastEditors
 * @LastEditTime : 2022-10-27 17:38:59
 * @FilePath     : \snow-frontend-pc\src\utils\runwork\index.ts
 */
import axios from "axios";
import { cloneDeep } from "lodash-es";
export class RunWork {
  private static async getAppAccessToken() {
    const data = await axios({
      url: "open-apis/auth/v3/app_access_token/internal",
      method: "post",
      data: {
        app_id: "cli_a3d218d07cf8d08f",
        app_secret: "7FsEkEsKmGJ1Mnwn0Qd5OgHKk0iRnNgU"
      }
    });
    return data.data.app_access_token;
  }

  public static async sendMsg(
    title: string,
    userId: string,
    applicant: Array<string>,
    jumpUrl: string
  ) {
    const token = await this.getAppAccessToken();
    const cardMsg = cloneDeep(this.cardMsg);
    cardMsg.header.title.content = `[雪花工程管理平台]${title}`;
    applicant.forEach(el => {
      cardMsg.elements[0].fields.push({
        is_short: false,
        text: {
          tag: "lark_md",
          content: el
        }
      });
    });
    cardMsg.elements[2].extra.url = jumpUrl;
    return axios({
      url: "open-apis/im/v1/messages?receive_id_type=user_id",
      method: "post",
      data: {
        receive_id: userId,
        content: JSON.stringify(cardMsg),
        msg_type: "interactive"
      },
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
  }

  private static cardMsg = {
    config: {
      wide_screen_mode: true
    },
    header: {
      template: "yellow",
      title: {
        tag: "plain_text",
        content: "[雪花工程管理平台]xxx的到货验收"
      }
    },
    elements: [
      {
        tag: "div",
        fields: [
          {
            is_short: true,
            text: {
              tag: "lark_md",
              content: '**提交人：**\n<at id="guoxi9">郭曦</at>'
            }
          },
          {
            is_short: false,
            text: {
              tag: "lark_md",
              content: ""
            }
          },
          {
            is_short: false,
            text: {
              tag: "lark_md",
              content: "**审批事由：**"
            }
          }
        ]
      },
      {
        tag: "hr"
      },
      {
        tag: "div",
        text: {
          tag: "lark_md",
          content: ""
        },
        extra: {
          tag: "button",
          text: {
            tag: "lark_md",
            content: "查看详情"
          },
          type: "primary",
          url: "https://feishu.cn"
        }
      }
    ]
  };
}
